package cn.exrick.xboot.modules.ApprovalInfo.serviceimpl;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.utils.jiuqi.utils.JiGuangPushUtil;
import cn.exrick.xboot.config.pushCode.jPushConfig;
import cn.exrick.xboot.modules.ApprovalInfo.dao.mapper.ApprovalInfoMapper;
import cn.exrick.xboot.modules.ApprovalInfo.entity.AppApproval;
import cn.exrick.xboot.modules.ApprovalInfo.entity.ApprovalInfo;
import cn.exrick.xboot.modules.ApprovalInfo.service.IApprovalInfoService;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 审批信息表接口实现
 * @author njp
 */
@Slf4j
@Service
@Transactional
public class IApprovalInfoServiceImpl extends ServiceImpl<ApprovalInfoMapper, ApprovalInfo> implements IApprovalInfoService {

    @Autowired
    private ApprovalInfoMapper approvalInfoMapper;

    @Autowired
    private jPushConfig config;
    /**
     * 三权分立查询超级管理员登录的审批数据精确查询
     * @param approvalInfo
     * @return
     */
    public List<ApprovalInfo> selectApprovalInfo(ApprovalInfo approvalInfo){
        return approvalInfoMapper.selectApprovalInfo(approvalInfo);
    }
    /**
     * 三权分立查询超级管理员登录的审批数据精确查询(分页)
     * @param approvalInfo
     * @return
     */
    public IPage<ApprovalInfo> selectApprovalInfo(Page page,@Param("approvalInfo") ApprovalInfo approvalInfo){
        return approvalInfoMapper.selectApprovalInfo(page,approvalInfo);
    }

    /**
     * 三权分立查询超级管理员登录的审批数据模糊查询
     * @param approvalInfo
     * @return
     */
    public IPage<ApprovalInfo> approvalInfoFuzzyQuery(Page page, ApprovalInfo approvalInfo){
        return approvalInfoMapper.approvalInfoFuzzyQuery(page,approvalInfo);
    }

    /**
     * 查询当前过期时间小于当前时间的各个状态的数据
     * @param approvalInfo
     * @return
     */
    public List<ApprovalInfo> selectApprovalInfoByAgo(ApprovalInfo approvalInfo){
        return approvalInfoMapper.selectApprovalInfoByAgo(approvalInfo);
    }

    /**
     * 向APP 发送审批数据(调用此方法只需确定登录用户是审批员即可，是否有审批数据方法,内部会做判断)
     * @param
     * @return
     */
    @Override
    public Boolean sendApprovalInfoToAPP(String   username) {
        boolean flag=false;
        ApprovalInfo approvalInfo=new ApprovalInfo();
        approvalInfo.setNowTime(new Date());
        approvalInfo.setApprovalStatus(CommonConstant.APPROVAL_INIT);//3:待审批
        //查询待审批数据
        List<ApprovalInfo> approvalInfos = selectApprovalInfo(approvalInfo);
        if(approvalInfos.size()>0){
            //如果有待审批数据取最新的数据
            ApprovalInfo approvalInfo1 = approvalInfos.get(0);
            //approvalInfo1.setUsername("admin");
            //String jsonInfo = JSONObject.toJSONString(javaToJSON(approvalInfo1, true, "200", "待审批"));
            AppApproval appApproval=new AppApproval();
            appApproval.setId(approvalInfo1.getId());
            appApproval.setMessage(CommonConstant.APP_CONTENT);
            appApproval.setUsername(CommonConstant.PARAM_USERNAME);
            appApproval.setCreateTime(approvalInfo1.getCreateTime());
            Map<String, String> appMap = getAppMap(appApproval);
            //开始向APP发送消息
            JPushClient jPushClient = new JPushClient(config.getMasterSecret(),config.getAppKey());
            PushResult pushResult=JiGuangPushUtil.pushAPP(jPushClient,"alias",username,appMap,CommonConstant.APP_ALERT,"LoginApprovals");
            if (pushResult.statusCode != 1&&pushResult.statusCode != 4){
                flag=true;
                log.info("推送成功"+username);
            }else {
                flag=false;
            }
    }
        return flag;
    }

    public  Map<String,String> getAppMap(AppApproval appApproval) {
        Map map=new HashMap() ;
        map.put("body" ,JSONObject.toJSONString(appApproval));//对象数据
        System.out.println("json"+JSONObject.toJSONString(appApproval, SerializerFeature.WriteDateUseDateFormat));//格式化时间
        //map.put("admin" ,CommonConstant.PARAM_USERNAME);//管理员账号
        map.put("RequestType" ,CommonConstant.APPRequestType);//推送业务数据类型
        return  map;
    }
   /* public static JSONObject javaToJSON(ApprovalInfo approvalInfo, Boolean flag, String code, String message) {
        if(flag==null){
            flag=true;
        }
        if(code==null){
            code="200";
        }
        if(message==null){
            message="success";
        }
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("success",flag);
        jsonObj.put("code", code);
        jsonObj.put("message", message);
        jsonObj.put("result", approvalInfo);
        jsonObj.put(CommonConstant.APPROVAL_UPDATE, true);
       *//* Map map = new HashMap();
        map.put("success",flag);
        map.put("code",code);
        map.put("message",message);
        map.put("result",approvalInfo);
        map.put(CommonConstant.APPROVAL_UPDATE,true);
        jsonObj.put("EXTRA_EXTRA", map);*//*
        System.out.println("java--->json \n" + jsonObj.toString());
        return jsonObj;
    }*/
}